package com.jqq.homework;

/**
 * @author 姬倩倩
 * @version 1.0
 */
public class Homework06 {
    public static void main(String[] args) {
        Person tangSeng = new Person("唐僧",new Horse());
        tangSeng.passRoad();
        tangSeng.passRiver();
        tangSeng.passFireMountain();
    }
}
interface Vehicles{
    void work();
}
class Horse implements Vehicles{
    @Override
    public void work() {
        System.out.println("一般情况下，骑白龙马前进。。");
    }
}
class Boat implements Vehicles{
    @Override
    public void work() {
        System.out.println("过河的时候，坐船前进。。");
    }
}
class Airplane implements Vehicles{
    @Override
    public void work() {
        System.out.println("路过火焰山的时候，坐飞机前进。。");
    }
}
class VehicleFactory{
    private VehicleFactory(){}
    private static Horse horse = new Horse();
    private static Boat boat = new Boat();
    private static Airplane airplane = new Airplane();

    public static Horse getHorse(){
        return horse;
    }
    public static Boat getBoat(){
        return boat;
    }
    public static Airplane getAirplane(){
        return airplane;
    }

}
class Person{
    private String name;
    private Vehicles vehicles;

    //在创建person对象时，要先给一个交通工具
    public Person(String name, Vehicles vehicles) {
        this.name = name;
        this.vehicles = vehicles;
    }
    public void passRiver(){
        //判断是否有当前需要的交通工具，如果没有，就new一个
        if(!(vehicles instanceof Boat)){
            vehicles = VehicleFactory.getBoat();
        }
        vehicles.work();
    }

    public void passRoad(){
        //判断是否有当前需要的交通工具，如果没有，就new一个
        if(!(vehicles instanceof Horse)){
            vehicles = VehicleFactory.getHorse();
        }
        vehicles.work();
    }

    public void passFireMountain(){
        //判断是否有当前需要的交通工具，如果没有，就new一个
        if(!(vehicles instanceof Airplane)){
            vehicles = VehicleFactory.getAirplane();
        }
        vehicles.work();
    }

}